﻿// =================================================================== 
// 项目说明
//====================================================================
// SunCity@Copy Right 2006-2008
// 文件： Category.cs
// 项目名称：SunCity_MS
// 创建时间：2015/3/18
// 负责人：Chris
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using EMS20.Entity;
using EMS20.IDataAccessLayer;

namespace EMS20.DataAccessLayer
{
    /// <summary>
    /// 数据层实例化接口类 dbo.Category.
    /// </summary>
    public partial class CategoryDataAccessLayer : ICategoryDataAccessLayer
    {
		#region 构造函数
		/// <summary>
		/// ??????
		/// </summary>
		public CategoryDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_CategoryModel">Category实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_CategoryInsert(CategoryEntity _CategoryModel)
		{
			string sqlStr="CreateUpdateDelete_CategoryEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@c_id",SqlDbType.Int),
			new SqlParameter("@c_type",SqlDbType.Int),
			new SqlParameter("@c_title",SqlDbType.VarChar),
			new SqlParameter("@parent_id",SqlDbType.Int)
			};
			_param[0].Value=0;
			_param[1].Value=_CategoryModel.c_id;
			_param[2].Value=_CategoryModel.c_type;
			_param[3].Value=_CategoryModel.c_title;
			_param[4].Value=_CategoryModel.parent_id;
			res = (int)SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_CategoryModel">Category实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_CategoryInsert(SqlTransaction sp,CategoryEntity _CategoryModel)
		{
			string sqlStr="CreateUpdateDelete_CategoryEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@c_id",SqlDbType.Int),
			new SqlParameter("@c_type",SqlDbType.Int),
			new SqlParameter("@c_title",SqlDbType.VarChar),
			new SqlParameter("@parent_id",SqlDbType.Int)
			};
			_param[0].Value=0;
			_param[1].Value=_CategoryModel.c_id;
			_param[2].Value=_CategoryModel.c_type;
			_param[3].Value=_CategoryModel.c_title;
			_param[4].Value=_CategoryModel.parent_id;
			res = (int)SqlHelper.ExecuteScalar(sp,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}


		/// <summary>
		/// 向数据表Category更新一条记录。
		/// </summary>
		/// <param name="_CategoryModel">_CategoryModel</param>
		/// <returns>影响的行数</returns>
		public int Create_CategoryUpdate(CategoryEntity _CategoryModel)
		{
            string sqlStr="CreateUpdateDelete_CategoryEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@c_id",SqlDbType.Int),
				new SqlParameter("@c_type",SqlDbType.Int),
				new SqlParameter("@c_title",SqlDbType.VarChar),
				new SqlParameter("@parent_id",SqlDbType.Int)
				};
				_param[0].Value=1;
				_param[1].Value=_CategoryModel.c_id;
				_param[2].Value=_CategoryModel.c_type;
				_param[3].Value=_CategoryModel.c_title;
				_param[4].Value=_CategoryModel.parent_id;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表Category更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_CategoryModel">_CategoryModel</param>
		/// <returns>影响的行数</returns>
		public int Create_CategoryUpdate(SqlTransaction sp,CategoryEntity _CategoryModel)
		{
            string sqlStr="CreateUpdateDelete_CategoryEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@c_id",SqlDbType.Int),
				new SqlParameter("@c_type",SqlDbType.Int),
				new SqlParameter("@c_title",SqlDbType.VarChar),
				new SqlParameter("@parent_id",SqlDbType.Int)
				};
				_param[0].Value=1;
				_param[1].Value=_CategoryModel.c_id;
				_param[2].Value=_CategoryModel.c_type;
				_param[3].Value=_CategoryModel.c_title;
				_param[4].Value=_CategoryModel.parent_id;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Category中的一条记录
		/// </summary>
	    /// <param name="c_id">c_id</param>
		/// <returns>影响的行数</returns>
		public int Create_CategoryDelete(int c_id)
		{
        CategoryEntity _CategoryModel =new CategoryEntity();
			string sqlStr="CreateUpdateDelete_CategoryEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.VarChar),
			new SqlParameter("@c_id",SqlDbType.Int),			
			new SqlParameter("@c_type",SqlDbType.Int),
			new SqlParameter("@c_title",SqlDbType.VarChar),
			new SqlParameter("@parent_id",SqlDbType.Int)
			};
			_param[0].Value=2;
			_param[1].Value=c_id;
			_param[2].Value=_CategoryModel.c_type;
			_param[3].Value=_CategoryModel.c_title;
			_param[4].Value=_CategoryModel.parent_id;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Category中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="c_id">c_id</param>
		/// <returns>影响的行数</returns>
		public int Create_CategoryDelete(SqlTransaction sp,int c_id)
		{
         CategoryEntity _CategoryModel =new CategoryEntity();
			string sqlStr="CreateUpdateDelete_CategoryEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@c_id",SqlDbType.Int),
			new SqlParameter("@c_type",SqlDbType.Int),
			new SqlParameter("@c_title",SqlDbType.VarChar),
			new SqlParameter("@parent_id",SqlDbType.Int)
			};
			_param[0].Value=2;
			_param[1].Value=c_id;
			_param[2].Value=_CategoryModel.c_type;
			_param[3].Value=_CategoryModel.c_title;
			_param[4].Value=_CategoryModel.parent_id;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  category 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>category 数据实体</returns>
		public CategoryEntity Populate_CategoryEntity_FromDr(DataRow row)
		{
			CategoryEntity Obj = new CategoryEntity();
			if(row!=null)
			{
				Obj.c_id = (( row["c_id"])==DBNull.Value)?0:Convert.ToInt32( row["c_id"]);
				Obj.c_type = (( row["c_type"])==DBNull.Value)?0:Convert.ToInt32( row["c_type"]);
				Obj.c_title =  row["c_title"].ToString();
				Obj.parent_id = (( row["parent_id"])==DBNull.Value)?0:Convert.ToInt32( row["parent_id"]);
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  category 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>category 数据实体</returns>
		public CategoryEntity Populate_CategoryEntity_FromDr(IDataReader dr)
		{
			CategoryEntity Obj = new CategoryEntity();
			
				Obj.c_id = (( dr["c_id"])==DBNull.Value)?0:Convert.ToInt32( dr["c_id"]);
				Obj.c_type = (( dr["c_type"])==DBNull.Value)?0:Convert.ToInt32( dr["c_type"]);
				Obj.c_title =  dr["c_title"].ToString();
				Obj.parent_id = (( dr["parent_id"])==DBNull.Value)?0:Convert.ToInt32( dr["parent_id"]);
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个Category对象
		/// </summary>
		/// <param name="c_id">c_id</param>
		/// <returns>Category对象</returns>
		public CategoryEntity Get_CategoryEntity (int c_id)
		{
			CategoryEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@c_id",SqlDbType.Int)
			};
			_param[0].Value=c_id;
			string sqlStr="select * from Category where c_id=@c_id";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_CategoryEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表Category所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< CategoryEntity> Get_CategoryAll()
		{
			IList< CategoryEntity> Obj=new List< CategoryEntity>();
			string sqlStr="select * from Category";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_CategoryEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="c_id">c_id</param>
        /// <returns>是/否</returns>
		public bool IsExistCategory(int c_id)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@c_id",SqlDbType.Int)
                                  };
            _param[0].Value=c_id;
            string sqlStr="select Count(*) from Category where c_id=@c_id";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
